﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBroker.Criteria;
using DataBroker.Dal;
using NorthwindDAO;
using NUnit.Framework;

namespace D2BkTest
{
    [TestFixture]
    public class UpdateTest
    {
        [SetUp]
        public void SetUp()
        {
            D2Bk.ChangeDatabase("Northwind");
        }
        [Test]
        public void UpdateSingleRow()
        {
            //Get city of Customer with id = ALFKI
            var f = new Filter();
            f.Add("Customerid", "ALFKI");
            var customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(1, customers.Count);
            
            var customer = customers[0] as Customers;
            Assert.IsNotNull(customer);
            
            //Save current city to restore later
            string saveCity = customer.City;
            customer.City = "Saint Paul";
            //Save city
            D2Bk.SaveOne(customer);
            
            //Retrieve customer once more
            customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(1, customers.Count);
            customer = customers[0] as Customers;
            if (customer != null)
            {
                Assert.AreEqual("Saint Paul", customer.City);
                customer.City = saveCity;
                D2Bk.SaveOne(customer);
            }
        }
    }
}
